package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.chargePayment.entity.req.SmsWnsChargePaymentRecordReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.ChargeItemBatchAddReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsChargePlanStudentStatusExportReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsIdListReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.resp.SmsWnsChargePlanStudentStatusResp;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsChargePlanStudentStatusService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 收费计划学生缴费状态表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-02-21
 */
@RestController
@RequestMapping("/welcomeNewStudent/chargePlanStudentStatus")
@SecretBody
public class SmsWnsChargePlanStudentStatusController {

    @Resource
    private SmsWnsChargePlanStudentStatusService smsWnsChargePlanStudentStatusService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("查看缴费记录")
    @GetMapping("/paymentRecords")
    public ResponseResult getPaymentRecords(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "planId", required = false) String planId,
                                            @RequestParam(value = "categoryId", required = false) String categoryId,
                                            @RequestParam(value = "categoryDetailId", required = false) String categoryDetailId,
                                            @RequestParam(value = "stuName", required = false) String stuName,
                                            @RequestParam(value = "orgId", required = false) String orgId,
                                            @RequestParam(value = "isPaid", required = false) Boolean isPaid) {
        return smsWnsChargePlanStudentStatusService.getPaymentRecords(pageNum, pageSize, planId, categoryId, categoryDetailId,
                stuName, orgId, isPaid);
    }

    @ApiOperation("缴费明细记录导出")
    @PostMapping("/batch/export")
    public ResponseResult exportPaymentRecords(@RequestBody SmsWnsChargePlanStudentStatusExportReq exportReq, HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsWnsChargePlanStudentStatusService.exportPaymentRecords(exportReq, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation("学生合计缴费记录批量导出")
    @PostMapping("/batch/sumExport")
    public ResponseResult exportPaymentSumRecords(@RequestBody SmsWnsChargePlanStudentStatusExportReq exportReq, HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsWnsChargePlanStudentStatusService.exportPaymentSumRecords(exportReq, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation("个人缴费情况")
    @GetMapping("/listStuStatus")
    public ResponseResult listStuStatus(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam("planId") String planId,
                                        @RequestParam(value = "orgId", required = false) Integer orgId,
                                        @RequestParam(value = "searchStr", required = false) String searchStr) {
        return smsWnsChargePlanStudentStatusService.listStuStatus(pageNum, pageSize, planId, orgId, searchStr);
    }

    @ApiOperation("修改应缴金额")
    @PostMapping("/updNeedPayById")
    public ResponseResult updNeedPayById(@RequestBody SmsWnsChargePlanStudentStatusResp req) {
        return smsWnsChargePlanStudentStatusService.updNeedPayById(req);
    }

    @ApiOperation("个人缴费情况")
    @GetMapping("/list")
    public ResponseResult list(HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsChargePlanStudentStatusService.list(userId);
    }

    @ApiOperation("某计划个人缴费情况")
    @GetMapping("/listByPlanId")
    public ResponseResult listByPlanId(@RequestParam("planId") String planId, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsChargePlanStudentStatusService.listByPlanId(planId, userId);
    }

    @ApiOperation("选择缴费项")
    @PostMapping("/chooseStatus")
    public ResponseResult chooseStatus(@RequestBody SmsWnsIdListReq req) {
        return smsWnsChargePlanStudentStatusService.chooseStatus(req);
    }

    @ApiOperation("缴费清单")
    @GetMapping("/paymentList")
    public ResponseResult paymentList(@RequestParam("type") Integer type,
                                      HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsChargePlanStudentStatusService.paymentList(type, userId);
    }

    @ApiOperation("导出某计划所有学生应缴项")
    @PostMapping("/batch/stuStatusExport")
    public ResponseResult stuStatusExport(@RequestBody SmsWnsChargePlanStudentStatusExportReq exportReq, HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsWnsChargePlanStudentStatusService.stuStatusExport(exportReq, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation(value = "导入某计划所有学生应缴项")
    @PostMapping("/newStu/batch/add")
    public ResponseResult batchAddStuStatus(@RequestBody ChargeItemBatchAddReq batchAddReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsWnsChargePlanStudentStatusService.batchUpdStuStatus(batchAddReq, userId);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiOperation("一键催款")
    @PostMapping("/pressForMoney")
    public ResponseResult pressForMoney(HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsChargePlanStudentStatusService.pressForMoney(userId);
    }

    @ApiOperation("学生欠缴费情况导出")
    @PostMapping("/batch/allStuStatusExport")
    public ResponseResult allStuStatusExport(HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsWnsChargePlanStudentStatusService.allStuStatusExport(response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiOperation("线下缴费（老师录入）")
    @PostMapping("/offlinePayment")
    public ResponseResult offlinePayment(@RequestBody SmsWnsChargePaymentRecordReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWnsChargePlanStudentStatusService.offlinePayment(req, userId);
    }

}

